<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\Service\HandleConfirmOrder;
use App\Admin\Forms\Service\HandleGoodOrder;
use App\Admin\Forms\Service\HandleRefund;
use App\Admin\Repositories\GoodOrder;
use App\Admin\Repositories\GoodOrderDetail;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Contracts\Support\Renderable;

class GoodOrderController extends AdminController
{

    /**
     * Make a grid builder.
     *
     */
    protected function grid()
    {
        $row = new Row;
        $tab = Tab::make();
        $m = request()->input('m',0);
        $count = \App\Models\GoodOrder::query()->where("is_apply",0)->where("state",1)->count();
        $tab->addLink("待发货订单({$count})", url('admin/good-order')."?m=0", intval($m)==0);
        $count1 = \App\Models\GoodOrder::query()->where("is_apply",0)->where("state",2)->count();
        $tab->addLink("待收货订单({$count1})", url('admin/good-order')."?m=1", intval($m)==1);
        $count2 = \App\Models\GoodOrder::query()->where("is_apply",0)->whereIn("state",[3,4])->count();
        $tab->addLink("已完成订单({$count2})", url('admin/good-order')."?m=2",intval($m)==2);
        $count3 = \App\Models\GoodOrder::query()->where("is_apply",1)->count();
        $tab->addLink("待退款订单({$count3})", url('admin/good-order')."?m=3",intval($m)==3);
        $count4 = \App\Models\GoodOrder::query()->where("state",5)->count();
        $tab->addLink("已退款订单({$count4})", url('admin/good-order')."?m=4",intval($m)==4);
        $row->column(12,$tab);
        if ($m==0){
            $row->column(12,$this->order());
        }elseif ($m==1){
            $row->column(12,$this->wait1());
        }elseif ($m==2){
            $row->column(12,$this->finish());
        }elseif ($m==3){
            $row->column(12,$this->waitRefund());
        }else{
            $row->column(12,$this->refund());
        }
        return Card::make($row);
    }

    protected function makeGrid(\Closure $callback): Grid
    {
        return Grid::make(new GoodOrder(['shop','user']), function (Grid $grid) use ($callback) {
            $grid->disableCreateButton();
            $grid->disableActions();
            $grid->disableDeleteButton();
            $grid->disableRowSelector();
            $this->common($grid);
            $callback($grid);
            $grid->column('state','订单状态')->using(\App\Models\GoodOrder::getStateList())->label(['default','primary','success','success','warning','danger']);
            $grid->column('action','操作')->display(function (){
                $url = url('admin/good-order')."/".$this->id."/edit";
               return "<a  href='{$url}' ><span class='label bg-info'>详情</span></a>";
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('order_no');
                $filter->equal('pay_way')->select(['余额支付','微信支付']);
                $filter->equal('method')->select(['快递配送','到店自提']);
                $filter->like('address->name','收件人名称');
                $filter->like('address->phone','收件人电话');
                $filter->like('address->address','收件地址');
            });
            $this->export($grid);
        });
    }

    protected function export(Grid $grid){
        $titles = [
            'order_no' => '订单编号','pay_way_text'=>"支付方式","method_text"=>"收货方式", 'nickname' => '用户昵称',
            'amount'=>'支付金额','name'=>'收件人名称','phone'=>'收件人电话','address'=>'收件地址','order_time'=>'下单时间',
            'pay_time'=>'支付时间','state_text'=>'订单状态','deliver_text'=>'发货方式','shop_name'=>"发货方"
        ];
        $grid->export()->rows(function ($rows) {
            $stateList = \App\Models\GoodOrder::getStateList();
            $delivers = ['待安排','平台发货','门店发货'];
            foreach ($rows as $index => &$row) {
                $row['shop_name'] = $row['shop']?$row['shop']['name']:"平台";
                $row['nickname'] = $row['user']?$row['user']['nickname']:"";
                $row['name'] = $row['address']?$row['address']['name']:"";
                $row['phone'] = $row['address']?$row['address']['phone']:"";
                $row['address'] = $row['address']?$row['address']['address']:"";
                $row['pay_way_text'] =  $row['pay_way']?"微信支付":"余额支付";
                $row['method_text'] =  $row['method']?"到店自提":"快递配送";
                $row['deliver_text'] =  $delivers[$row['deliver']];
                $row['state_text'] = $stateList[$row['state']];
            }
            return $rows;
        })->titles($titles);
    }

    protected function common(Grid $grid){
        $grid->column('order_no');
        $grid->column('pay_way')->using(['余额支付','微信支付'])->label(['primary','info']);
        $grid->column('amount');
        $grid->column('method')->using(['快递配送','到店自提'])->label(['primary','info']);
        $grid->column('user.nickname','用户昵称');
        $grid->column('address.name','收件人名称');
        $grid->column('address.phone','收件人电话');
        $grid->column('address.address','收件地址');
        $grid->column('order_time')->sortable();
        $grid->column('pay_time')->sortable();
        $grid->column('deliver')->using(['待安排','平台发货','门店发货'])->label(['primary','info','success']);
        $grid->column('shop.name','发货方')->display(function ($value){
            $state = $this->state;
            $is_apply = $this->is_apply;
            return $state>0&&$state<5&&$is_apply==0?($value?:"平台"):"-";
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function order()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("is_apply",0)->where("state",1);
//            $m = request()->input('m',0);
//            $grid->wrap(function (Renderable $view)  use ($m) {
//                $tab = Tab::make();
////                $count = \App\Models\GoodOrder::query()->where("is_apply",0)->where("state",1)->count();
////                $tab->add("待发货订单({$count})", $view, true);
////                $count1 = \App\Models\GoodOrder::query()->where("is_apply",0)->where("state",2)->count();
////                $tab->add("待收货订单({$count1})", $this->wait1());
////                $count2 = \App\Models\GoodOrder::query()->where("is_apply",0)->whereIn("state",[3,4])->count();
////                $tab->add("已完成订单({$count2})", $this->finish());
////                $count3 = \App\Models\GoodOrder::query()->where("is_apply",1)->count();
////                $tab->add("待退款订单({$count3})", $this->waitRefund());
////                $count4 = \App\Models\GoodOrder::query()->where("state",5)->count();
////                $tab->add("已退款订单({$count4})", $this->refund());
//
//                $count = \App\Models\GoodOrder::query()->where("is_apply",0)->where("state",1)->count();
//                $tab->addLink("待发货订单({$count})", url('admin/good-order')."?m=0", intval($m)==0);
//                $count1 = \App\Models\GoodOrder::query()->where("is_apply",0)->where("state",2)->count();
//                $tab->addLink("待收货订单({$count1})", url('admin/good-order')."?m=1", intval($m)==1);
//                $count2 = \App\Models\GoodOrder::query()->where("is_apply",0)->whereIn("state",[3,4])->count();
//                $tab->addLink("已完成订单({$count2})", url('admin/good-order')."?m=2",intval($m)==2);
//                $count3 = \App\Models\GoodOrder::query()->where("is_apply",1)->count();
//                $tab->addLink("待退款订单({$count3})", url('admin/good-order')."?m=3",intval($m)==3);
//                $count4 = \App\Models\GoodOrder::query()->where("state",5)->count();
//                $tab->addLink("已退款订单({$count4})", url('admin/good-order')."?m=4",intval($m)==4);
//                return $tab;
//            });

            $grid->column('state0','发货操作')->display(function (){
                return "发货";
            })->modal("安排发货",function ($model){
                $model->xl();
                return HandleGoodOrder::make()->payload(['oid'=>$this->id]);
            });

        });
    }


    /**
     * 代收货
     *
     * @return Grid
     */
    protected function wait1()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("is_apply",0)->where("state",2);
            $grid->column('state1','确认收货')->display(function (){
                return "确认收货";
            })->modal("确认收货",function ($model){
                $model->xl();
                return HandleConfirmOrder::make()->payload(['oid'=>$this->id]);
            });
        });
    }

    /**
     * 已完成
     *
     * @return Grid
     */
    protected function finish()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("is_apply",0)->whereIn("state",[3,4]);
        });
    }

    /**
     * 待退款
     *
     * @return Grid
     */
    protected function waitRefund()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("is_apply",1);
            $grid->column('state3','审核操作')->display(function (){
                return "审核退款";
            })->modal("审核退款",function ($model){
                $model->xl();
                return HandleRefund::make()->payload(['oid'=>$this->id]);
            });
        });
    }

    /**
     * 待退款
     *
     * @return Grid
     */
    protected function refund()
    {
        return $this->makeGrid(function (Grid $grid){
            $grid->model()->where("state",5);
        });
    }



    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GoodOrder(['user']), function (Form $form) {
            $form->column(12,function (Form $form){
                $model = $form->model();
                $form->html($this->gridDetail($model->order_no),"订单详情");
            });
            $form->column(6,function (Form $form){
                $form->display('order_no');
                $form->display('pay_way')->with(function ($v){
                    $types = ['余额支付','微信支付'];
                    return $types[$v];
                });
                $form->display('amount');
                $form->display('method')->with(function ($v){
                    $types = ['快递配送','到店自提'];
                    return $types[$v];
                });
                $form->display('deliver')->with(function ($v){
                    $types = ['待安排','平台发货','门店发货'];
                    return $types[$v];
                });
                $form->display('state','订单状态')->with(function ($v){
                    $types = \App\Models\GoodOrder::getStateList();
                    return $types[$v];
                });
            });
            $form->column(6,function (Form $form){
                $form->display('user.nickname','用户昵称');
                $form->display('address.name','收件人名称');
                $form->display('address.phone','收件人电话');
                $form->display('address.address','收件地址');
                $form->display('order_time');
                $form->display('pay_time');
                $form->display('send_time');
                $form->display('finish_time','完成时间');
            });
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function gridDetail($oid)
    {
        return Grid::make(new GoodOrderDetail(), function (Grid $grid) use ($oid) {
            $grid->disableCreateButton();
            $grid->disableColumnSelector();
            $grid->disableActions();
            $grid->disableRefreshButton();
            $grid->disableRowSelector();
            $grid->disablePagination();
            $grid->column('id')->sortable();
            $grid->model()->where("oid",$oid);
            $grid->column('good_id','商品ID');
            $grid->column('good_name','商品名称');
            $grid->column('good_image','商品图片')->image(null,40,40);
            $grid->column('good_number','商品数量');
            $grid->column('good_price','商品单价');
            $grid->column('total_price','小计');
        });
    }
}
